(function ($) {
    $.fn.extend({
        //插件名称 
        loginDialog: function (options) {

            //参数和默认值
            var defaults = {
            };

            var options = $.extend(defaults, options);
            
            
            
            return this.each(function () {
				var showDialogBt = this;
                var loginDialog = {
               		generateActivationCodeBtnId:"generateActivationCodeBtn",
               		
               		registerBtnId:"registerBtn",
               		
               		userloginBtnId:"userloginBtn",
                	
               		userLoginForm : "userLoginForm",
               		
               		registerForm : "registerForm",
               		
               		activationCodeSendAbleTimes : 3,
               		
                    init:function() {
                    	 $('input[placeholder]').placeholder({ 
               		      useNative: true,  
               		      hideOnFocus: false,  
               		      style: {  
               		           textShadow: 'none'  
               		      }  
               		 	}); 
               		 
	               		$('#login_dialog').tabify();
	               		$.validationEngine.ajaxConfig = {
	               			"ajaxPhoneOrEmailValidate":{
	               				"url" : "/Emall/front/reg_phoneOrEmailValidate",
	               				"alertTextOk" : "* 此帐号名称可以使用",
	               				"alertText" : "* 此名称已被其他人使用",
	               				"alertTextLoad" : "* 正在确认帐号名称是否可用"
	               			}
	               		};
	               		var validateOption = { 
	               			scroll:false,
	               			ajaxFormValidation: true,
	               			//binded:false,
	               			//showArrow:false,
	               			//promptPosition:"centerRight",
	               			autoHidePrompt : false,
	               			maxErrorsPerField:1,
	               			showOneMessage:true,
	               			addPromptClass:"formError-noArrow formError-text",
	               			onFieldSuccess : function (field) {
	               				
	               			},
	               			onFieldFailure : function (field) {
	               				
	               			},
	               			ajaxReturnIsSuccess:function (json) {
	               				if (json.result == 'SUCCESS') {
	               					return true;
	               				} else {
	               					return false;
	               				}
	               			}
	               		};
	               		$("#" + loginDialog.registerForm).validationEngine(validateOption);
	               		
	               		$("#"+this.generateActivationCodeBtnId).click(this.generateActivationCode);
	               		$("#"+this.registerBtnId).click(this.register);
	               		$("#"+this.userloginBtnId).click(this.userlogin);
	               		$(showDialogBt).click(this.showDialog);
                    },
                    
                    generateActivationCode :function(){
                		var time = $("#time").text();
                		if (time > 0) {
                			return;
                		}
                		
                		var valid = jQuery("#user_mobileOrMail").validationEngine("validate");
                		if (!valid) {
                			return;
                		}

                		var url = "/Emall/front/reg_sendActiveCode.action?mobileOrMaill="+$("#user_mobileOrMail").val();
                	   	$.getJSON(
                	  		url,
                	  		function(data) {
                	  			//$("#activeCodeLink_info").html('<a class="buttonS bBlue " href="javascript:generateActivationCode();">再次获取授权码</a>');
                				var result = data.result;
                	  			if(result == 'SUCCESS'){
                		        	$("#activeCodeLinkDiv a").html('<span>发送成功,<span id="time">60</span>秒以内无法再次发送</span>');
                		        	$("#activeCodeLinkDiv").removeClass("inableColor");
                		        	$("#activeCodeLinkDiv").addClass("disableColor");
                		        	var handler = window.setInterval(function() {
                		        		
                		        		 var time = $("#time").text();
                		        		 if (time > 0) {
                		        			$("#time").text(time - 1);
                		        		 } else {
                		        			window.clearInterval(handler);
                                            $("#activeCodeLinkDiv a").html('<span>发送授权码</span>');
                                            $("#activeCodeLinkDiv").removeClass("disableColor");
                                            //$("#activeCodeLinkDiv").addClass("inableColor");
                		        		 }

                		        	}, 1000);
                				}else{
                					$("#activeCodeLinkDiv").text("发送失败，请重新获取授权码");
                				}
                	  		}
                	  	);
                	},
                    
                	register : function() {
                        var valid = jQuery("#" + loginDialog.registerForm).validationEngine("validate");
                        if (!valid) {
                            return;
                        }

                        url = "/Emall/front/reg_register.action";
                        var myOption = {
                            url : url,
                            dataType : "json",
                            type : "post",
                            success : function(data) {
                            	layer.close(loginDialog._dialogIndex);
                                alert(data.content[0]);
                            }
                        };
                        $("#" + loginDialog.registerForm).ajaxSubmit(myOption);
                    },
                    
                    _dialogIndex : "",
                    
                    userlogin : function(){
                    	
                    	var myOption = {
                    			url : "/Emall/front/reg_loginForNewLoginDialog.action",
                    			type : "post",
                    			success : function(data) {
                    				var result = data.result;
                    				var content = data.content[0];
                    				if (result == 'SUCCESS') {
                    					layer.close(loginDialog._dialogIndex);
                    					alert(content);
                    				} else if (result == 'FAIL'){
                    					$("#loginMessage").text(content);
                    				}
                    			},
                    		dataType:"json"
                    	};
                    	$("#" + loginDialog.userLoginForm).ajaxSubmit(myOption);
                    },
                    
                    showDialog : function(){
                    	//清空验证提示信息及表单中的填写的内容
                    	$("#" + loginDialog.registerForm).validationEngine("hideAll");
                    	$("#" + loginDialog.registerForm + " input").val("").removeClass("error");
                    	$("#" + loginDialog.userLoginForm + " input").val("").removeClass("error");
                    	
                    	loginDialog._dialogIndex = $.layer({
                   		    type : 1,
                   		    shade : [0],
                   		    area : ['auto', 'auto'],
                   		    title : false,
                   		    border : [0],
                   		    page : {dom : '#login_dialog'}
                   		});
                    }
                };
                
                loginDialog.init();
            });
        }
    });})(jQuery);